<template>
  <el-dialog :title="headList" width="28%" :visible="showDialog" @close="close">
    <el-form
      ref="deptForm"
      :model="deptForm"
      :rules="rules"
    >

      <el-form-item v-if="!this.id" label="所属学科" prop="subjectID">
        <el-select v-model="deptForm.subjectID" placeholder="请选择" size="mini" style="width: 80%;">
          <el-option
            v-for="item in subjectList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-else></el-form-item>
      <el-form-item label="标签名称" prop="tagName">
        <el-input v-model="deptForm.tagName" placeholder="请输入目录名称" size="mini" style="width: 80%;" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="end">
          <el-col :span="12">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="confirmClick">确定</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { add, detail, update } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
export default {
  name: 'tagsAdd',
  data () {
    return {
      actionType: null, // 区别于是否是编辑，还是添加
      deptForm: {
        subjectID: +this.id,
        tagName: '',
        id: null
      },
      rules: {
        subjectID: [
          { required: true, message: '所属学科不能为空' }
        ],
        tagName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      },

      subjectList: []
    }
  },
  created () {
    this.getsubjectList()
    console.log(this.id)
  },
  computed: {
    headList () {
      return this.deptForm.id ? '修改标签' : '新增标签'
    }
  },
  methods: {
    // 通过id获取目录数据
    async getSelectionDetail () {
      this.deptForm.id = this.currentPageId
      const res = await detail(this.deptForm)
      this.deptForm = res.data
      // console.log(this.deptForm)
    },
    // 点击事件
    confirmClick () {
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          if (this.deptForm.id) {
            // 调用修改部门接口方法
            await update(this.deptForm)
            // 友好提示用户："修改部门成功"
            this.$message.success('修改部门成功')
          } else {
            await add(this.deptForm)
            this.$message.success('新增成功')
          }
          this.close()
        }
      })
    },
    async getsubjectList () {
      const res = await simple()
      this.subjectList = res.data
      // console.log(this.subjectList)
    },
    close () {
      this.deptForm = {
        subjectID: '',
        directoryName: '',
        id: null,
        subjectName: '',
        creator: '',
        addDate: null,
        totals: null,
        state: ''
      }
      this.$emit('update:showDialog', false)
      this.$emit('gest')
      this.$refs.deptForm.resetFields()
    }
  },
  props: {
    // 控制弹层显示和隐藏
    showDialog: {
      type: Boolean,
      default: false
    },
    // 父级id
    currentPageId: {
      type: Number,
      default: null
    },
    id: {
      default: null
    }
  }
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
